package com.dht.xiao.jianye.dayin.ui.fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.dht.xiao.jianye.R;
import com.dht.xiao.jianye.dayin.ErrorOrMsg;
import com.dht.xiao.jianye.dayin.MyApplication;
import com.dht.xiao.jianye.dayin.PrinterManager;
import com.dht.xiao.jianye.dayin.event.ConnectEvent;
import com.dht.xiao.jianye.dayin.util.ToastUtil;
import com.jolimark.printerlib.VAR.TransType;

import org.greenrobot.eventbus.EventBus;


/**
 * USB设置界面
 *
 * @author zhrjian
 *
 */
public class UsbSettingFragment extends Fragment implements View.OnClickListener {
	private Activity activity = null;
	private PrinterManager printerManager = null;
	private MyApplication myapplication;
	private Button btn_usb_connect, btn_usb_close;
//	private EditText edt_devpath;
	private String devPath = "/dev/usb/lp0";// 旧主板 双核cpu 热敏机芯判断路径
	private ProgressDialog m_pDialog; // 进度条

	@Override
	public void onAttach(Activity activity) {
		this.activity = activity;
		super.onAttach(activity);
		myapplication = (MyApplication) getActivity().getApplication();
        printerManager = myapplication.getPrinterManager();
}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// 注册EventBus
		EventBus.getDefault().register(this);
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_setting_usb, null);
		btn_usb_connect = (Button) view.findViewById(R.id.btn_usb_connect);
		btn_usb_close = (Button) view.findViewById(R.id.btn_usb_close);
//		edt_devpath = (EditText) view.findViewById(R.id.edt_devpath);
//		edt_devpath.setText(devPath);
		btn_usb_connect.setOnClickListener(this);
		btn_usb_close.setOnClickListener(this);

		initView();
		return view;
	}

	private void initView() {
		// 显示界面信息初始化
		if (printerManager.isPrinterReady()) {
			TransType ty = printerManager.getTransType();
			if (ty.equals(TransType.TRANS_USB)) {
				if (printerManager.isConnected()) {
					btn_usb_connect.setText(getString(R.string.connect_exist));
					btn_usb_connect.setTextColor(Color.GRAY);
					btn_usb_connect.setEnabled(false);
				}
//				edt_devpath.setText(printerManager.getAddress());
			}
		}
		// 创建连接进度对话框
		m_pDialog = new ProgressDialog(activity);
		m_pDialog.setMessage(getResources().getString(R.string.connecting));
		m_pDialog.setIndeterminate(false);
		m_pDialog.setCanceledOnTouchOutside(false);
	}

	@Override
	public void onDestroy() {
		EventBus.getDefault().unregister(this);
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_usb_connect:

			AlertDialog.Builder builder = new AlertDialog.Builder(activity);
			builder.setTitle(getString(R.string.select_connect_type));
			builder.setMessage(getString(R.string.connect_dialog_message));
			builder.setPositiveButton(getString(R.string.open), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					printerManager.setOpenTwoWayFlag(true);
					usbConnect();
				}
			});
			builder.setNegativeButton(getString(R.string.no_open), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					printerManager.setOpenTwoWayFlag(false);
					usbConnect();
				}
			});
			builder.show();
			break;
		case R.id.btn_usb_close: // 关闭连接
			int ret = printerManager.close();
			m_pDialog.dismiss();
			switch (ret) {
			case ErrorOrMsg.CLOSE_SUCCESSED:
				btn_usb_connect.setText(getString(R.string.wifi_connect));
				btn_usb_connect.setTextColor(getResources().getColor(R.color.whilte));
				btn_usb_connect.setEnabled(true);
				ToastUtil.show(activity, R.string.close_successed);
				break;
			case ErrorOrMsg.CLOSE_FAILED:
				ToastUtil.show(activity, R.string.close_failed);
				break;
			}
			break;
		}
	}
	private void usbConnect(){
		m_pDialog.show();// 显示连接进度条
//			devPath = edt_devpath.getText().toString().trim();
		// 初始化printerManager的连接信息
		printerManager.initRemotePrinter(TransType.TRANS_USB, devPath);
		// 建立连接
//			printerManager.connect();
		new Thread(){
			@Override
			public void run() {
				printerManager.connectUsbPrinter(activity);
			}
		}.start();
	}

	// 接收连接事件信息
		public void onEventMainThread(ConnectEvent connectEvent) {
			m_pDialog.dismiss();
			switch (connectEvent.msg) {
			case ErrorOrMsg.CONNECT_SUCCESSED:
				Log.d("tag", "-------------------------连接成功");
				ToastUtil.show(activity, R.string.connect_success);
				btn_usb_connect.setText(getString(R.string.connect_exist));
				btn_usb_connect.setTextColor(Color.GRAY);
				btn_usb_connect.setEnabled(false);
				break;
			case ErrorOrMsg.CONNECT_FAILED:
				Log.d("tag", "-------------------------连接失败");
				ToastUtil.show(activity, getString(R.string.connect_faile));
				break;
			case ErrorOrMsg.CONNECT_EXIST:
				Log.d("tag", "-------------------------已连接");
				ToastUtil.show(activity, R.string.connect_exist);
				break;
			case ErrorOrMsg.CONFIG_NULL:
				Log.d("tag", "-------------------------空");
				ToastUtil.show(activity, R.string.config_null);
				break;
				case ErrorOrMsg.FIND_USB_PRINTER:
					Log.d("tag", "-------------------------找到打印机");
					ToastUtil.show(activity, getString(R.string.find_printer));
					printerManager.close();
					break;
				case ErrorOrMsg.USB_PRINTER_STYLE_ERROR:
					Log.d("tag", "-------------------------获取打印机类型失败");
					ToastUtil.show(activity,getString(R.string.printer_type_failed));
					break;
			}
		}
}
